package com.ryan.time.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.ryan.time.R;
import com.ryan.time.biz.TimeBiz;
import com.ryan.util.BaseActivity;

public class TimeActivity extends BaseActivity {

	private EditText mEtTitle;
	private Spinner mSpCond;
	private Spinner mSpAction;
	private Spinner mSpResult;
	private Spinner mSpTimes;

	private ArrayAdapter<String> mCondAdapter;
	private ArrayAdapter<String> mActionAdapter;
	private ArrayAdapter<String> mResultAdapter;
	private ArrayAdapter<String> mTimesAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.time);

		mEtTitle = (EditText) findViewById(R.id.et_title);
		mSpCond = (Spinner) findViewById(R.id.sp_cond);
		mSpAction = (Spinner) findViewById(R.id.sp_action);
		mSpResult = (Spinner) findViewById(R.id.sp_result);
		mSpTimes = (Spinner) findViewById(R.id.sp_times);

		mCondAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, TimeBiz.sCond);
		mCondAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpCond.setAdapter(mCondAdapter);

		mActionAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, TimeBiz.sAction);
		mActionAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpAction.setAdapter(mActionAdapter);

		mResultAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, TimeBiz.sResult);
		mResultAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpResult.setAdapter(mResultAdapter);

		mTimesAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, TimeBiz.sTimes);
		mTimesAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpTimes.setAdapter(mTimesAdapter);

		processSubmit(mSubmitClick);
	}

	OnClickListener mSubmitClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			save();
		}

	};

	private void save() {

		TimeBiz.save(this, mEtTitle.getText().toString(),
				mSpCond.getSelectedItemPosition(),
				mSpAction.getSelectedItemPosition(),
				mSpResult.getSelectedItemPosition(),
				mSpTimes.getSelectedItemPosition());
	}
}
